<?php

/**
 * This is the model class for table "{{evaks}}".
 *
 * The followings are the available columns in table '{{evaks}}':
 * @property string $evak_id
 * @property string $evak_user
 * @property string $evak_phone1
 * @property string $evak_phone2
 * @property string $evak_driver
 * @property integer $evak_mark
 * @property integer $evak_type
 * @property integer $evak_mesto
 * @property string $evak_max_weigth
 * @property integer $evak_city
 * @property string $evak_added
 * @property string $evak_edited
 * @property string $driver_status
 * @property integer $evak_status
 */
class Evaks extends CActiveRecord {

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{evaks}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('evak_phone1, evak_driver, evak_city, evak_mark, evak_type, evak_max_weigth', 'required'),
            array('evak_mark, evak_type, evak_mesto, evak_city, evak_status', 'numerical', 'integerOnly' => true),
            array('evak_user, evak_phone1, evak_phone2, evak_driver, evak_max_weigth, evak_added, evak_edited, driver_status', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('evak_id, evak_user, evak_phone1, evak_phone2, evak_driver, evak_mark, evak_type, evak_mesto, evak_max_weigth, evak_city, evak_added, evak_edited, driver_status, evak_status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'city' => array(self::BELONGS_TO, 'Cities', 'evak_city'),
            'status' => array(self::BELONGS_TO, 'Evstats', 'evak_status'),
            'emark' => array(self::BELONGS_TO, 'Emarks', 'evak_mark'),
            'etype' => array(self::BELONGS_TO, 'Etypes', 'evak_type'),
            'weight' => array(self::BELONGS_TO, 'EvakWeight', 'evak_max_weigth'),
            'eprices' => array(self::HAS_MANY, 'Evak2price', 'evak_id'),
            'driverStatus' => array(self::BELONGS_TO, 'DriverStatus', 'driver_status'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'evak_id' => 'Evak',
            'evak_user' => 'Evak User',
            'evak_phone1' => 'Evak Phone1',
            'evak_phone2' => 'Evak Phone2',
            'evak_driver' => 'Evak Driver',
            'evak_mark' => 'Evak Mark',
            'evak_type' => 'Evak Type',
            'evak_mesto' => 'Evak Mesto',
            'evak_max_weigth' => 'Evak Max Weigth',
            'evak_city' => 'Evak City',
            'evak_added' => 'Evak Added',
            'evak_edited' => 'Evak Edited',
            'driver_status' => 'Driver Status',
            'evak_status' => 'Evak Status',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('evak_id', $this->evak_id, true);
        $criteria->compare('evak_user', $this->evak_user, true);
        $criteria->compare('evak_phone1', $this->evak_phone1, true);
        $criteria->compare('evak_phone2', $this->evak_phone2, true);
        $criteria->compare('evak_driver', $this->evak_driver, true);
        $criteria->compare('evak_mark', $this->evak_mark);
        $criteria->compare('evak_type', $this->evak_type);
        $criteria->compare('evak_mesto', $this->evak_mesto);
        $criteria->compare('evak_max_weigth', $this->evak_max_weigth, true);
        $criteria->compare('evak_city', $this->evak_city);
        $criteria->compare('evak_added', $this->evak_added, true);
        $criteria->compare('evak_edited', $this->evak_edited, true);
        $criteria->compare('driver_status', $this->driver_status, true);
        $criteria->compare('evak_status', $this->evak_status);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Evaks the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord) {
                $this->evak_user = Yii::app()->user->id;
                $this->evak_status = 0;
                $this->driver_status = 1;
                $this->evak_added = time();
            }
            $this->evak_edited = time();
            return true;
        }
        return FALSE;
    }

    public function afterSave() {
        if (isset($_POST['ptypes'])) {
            $criteria = new CDbCriteria();
            $criteria->compare('evak_id', $this->evak_id);
            Yii::app()->db->getCommandBuilder()->createDeleteCommand('{{evak2price}}', $criteria);
            foreach ($_POST['ptypes'] as $key => $val) {
                $data = array('evak_id' => $this->evak_id, 'ptype_id' => $val, 'price' => $_POST['ptype_price'][$key]);
                Yii::app()->db->getCommandBuilder()->createInsertCommand('{{evak2price}}', $data)->execute();
            }
        }
    }

}
